package com.cytech.cloudclinic.moudle.main.drug.add2.base;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.AddDrugTagMulAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.bean.AddDrugTag;
import com.cytech.cloudclinic.bean.event.UpdateAddTagEvent;
import com.cytech.cloudclinic.bean.req.AddDrugReq;
import com.cytech.cloudclinic.bean.resp.RespDrugDetail;
import com.cytech.cloudclinic.dialog.AddDrugTagDialog;
import com.cytech.cloudclinic.moudle.main.drug.add2.AddDrugActivity2;
import com.cytech.cloudclinic.utils.ConvertUtils;
import com.sly.pylibrary.Cn2Py;
import com.yechaoa.yutils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_2;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_5;
import static com.cytech.cloudclinic.app.Constant.MODE_TAG_ID;

public class AddDrugBaseFragment2 extends BaseFragment<AddDrugBasePresenter2> implements AddDrugBaseView2 {
    @BindView(R.id.et_add_drug_qr_code)
    EditText etAddDrugQrCode;
    @BindView(R.id.et_add_drug_name)
    EditText etAddDrugName;
    @BindView(R.id.et_add_drug_qr_name_py)
    EditText etAddDrugQrNamePy;
    @BindView(R.id.et_add_drug_qr_factory)
    EditText etAddDrugQrFactory;
    @BindView(R.id.et_add_drug_min_number)
    EditText etMinNumber;
    @BindView(R.id.et_add_drug_min_price)
    EditText etAddDrugMinPrice;

    @BindView(R.id.ll_add_drug_min_unit)
    LinearLayout llUnitLayout;
    @BindView(R.id.ll_add_drug_unit_1)
    LinearLayout llUnitLayout1;
    @BindView(R.id.ll_add_drug_unit_2)
    LinearLayout llUnitLayout2;

    //体质调理（早中晚3个单位）
    @BindView(R.id.et_add_drug_min_unit_1)
    EditText minUnit1;
    @BindView(R.id.et_add_drug_min_unit_2)
    EditText minUnit2;
    @BindView(R.id.et_add_drug_min_unit_3)
    EditText minUnit3;

    @BindView(R.id.tv_add_drug_min_unit)
    TextView tvMinUnit;

    private int drugTagId;
    private int drugId;
    private int parentDrugTagId;


    public static AddDrugBaseFragment2 newInstance(int drugTagId, int drugId, int parentDrugTagId) {
        AddDrugBaseFragment2 fragment = new AddDrugBaseFragment2();
        //如果有参数，使用Bundle传递
        Bundle bundle = new Bundle();
        bundle.putInt("drugTagId", drugTagId);
        bundle.putInt("drugId", drugId);
        bundle.putInt("parentDrugTagId", parentDrugTagId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected AddDrugBasePresenter2 createPresenter() {
        return new AddDrugBasePresenter2(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_drug_base2;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        etAddDrugName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.e("TAG", "afterTextChanged: " + editable.toString());
                if (!TextUtils.isEmpty(editable.toString())) {
                    etAddDrugQrNamePy.setText(Cn2Py.getIndexPinYin(editable.toString()).replace(",", "").toUpperCase());
                } else {
                    etAddDrugQrNamePy.setText("");
                }

            }
        });
    }

    @Override
    protected void initData() {
        drugTagId = getArguments().getInt("drugTagId");
        drugId = getArguments().getInt("drugId");
        parentDrugTagId = getArguments().getInt("parentDrugTagId");

        if(parentDrugTagId == DRUG_TAG_2){
            //体质调理
            llUnitLayout1.setVisibility(View.GONE);
            llUnitLayout2.setVisibility(View.VISIBLE);
        }else{
            llUnitLayout1.setVisibility(View.VISIBLE);
            llUnitLayout2.setVisibility(View.GONE);
        }

        String minUnit = presenter.getMinUnit(parentDrugTagId);
        tvMinUnit.setText(minUnit);

        if(drugId!=0){
            //不为0代表是查看、修改药品
            presenter.getDrugDetail(drugId);
        }
        //presenter.getSummaryList(5, parentDrugTagId);
    }


    @OnClick(R.id.btn_add_drug_next)
    public void onViewClicked() {
        AddDrugReq drug = new AddDrugReq();
        drug.drug_id = drugId;
        drug.drug_tag = drugTagId;
        drug.barcode = etAddDrugQrCode.getText().toString();
        drug.drug_name = etAddDrugName.getText().toString();
        drug.company = etAddDrugQrFactory.getText().toString();
        if(TextUtils.isEmpty(drug.drug_name)){
            ToastUtil.showToast("请输入药品名称");
            return;
        }
        drug.sub_price = ConvertUtils.toDouble(etAddDrugMinPrice.getText().toString());
        drug.subunit = presenter.getMinUnit(parentDrugTagId);
        if(parentDrugTagId == DRUG_TAG_2){
            //体质调理早中晚
            int unit1 = ConvertUtils.toInt(minUnit1.getText().toString());
            int unit2 = ConvertUtils.toInt(minUnit2.getText().toString());
            int unit3 = ConvertUtils.toInt(minUnit3.getText().toString());
            drug.numbers.add(unit1);
            drug.numbers.add(unit2);
            drug.numbers.add(unit3);
        }else{
            drug.number = ConvertUtils.toDouble(etMinNumber.getText().toString());
        }

        ((AddDrugActivity2)getActivity()).setRadioGroupSelect();
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment fragment = ((AddDrugActivity2)getActivity()).getDosageFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("addDrugReq",drug);
        bundle.putInt("drugTagId",drugTagId);
        bundle.putInt("drugId",drugId);
        bundle.putInt("parentDrugTagId",parentDrugTagId);
        fragment.setArguments(bundle);
        fm.beginTransaction().replace(R.id.fl_add_drug_container,fragment).commit();
    }


    /**
     * 获取药品详情
     * @param drugDetail
     */
    @Override
    public void getDrugDetailSuccess(RespDrugDetail drugDetail) {
        etAddDrugQrCode.setText(drugDetail.barcode);
        etAddDrugName.setText(drugDetail.drug_name);
        etAddDrugQrFactory.setText(drugDetail.company);
        etAddDrugQrNamePy.setText(Cn2Py.getIndexPinYin(drugDetail.drug_name).replace(",", "").toUpperCase());

        etAddDrugMinPrice.setText(String.valueOf(drugDetail.sub_price));
        etMinNumber.setText(String.valueOf(drugDetail.number));

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(UpdateAddTagEvent event) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
